use "data/iscap_wave6.dta", clear

** no missing
tab ethnicity
tab age 
tab education
tab region

decode romneytherm, gen(newromney)
replace newromney = "" if romneytherm<=3
destring newromney, replace

decode obamatherm, gen(newobama)
replace newobama = "" if obamatherm<=3
destring newobama, replace

drop romneytherm obamatherm

recode ideology12 (1=.) (2=1) (3=2) (4=3) (5=4) (6=5) (7=6) (8=7), gen(ideo12)

drop ideology12

recode prespref12 (1=.) (2=2) (3=1) (4=3) (5=.)
label values prespref12 .

label var prespref12 "1=Obama, 2=Romney, 3=Other"

merge 1:1 id using "data/iscap_wave7.dta"

gen nopost12 = _merge==1

drop _merge

gen novote2012 = 1 if vote12>=2 & vote12<=4
replace novote2012 = 0 if vote12 ==5
drop vote12

gen presvote2012 = . 
replace presvote2012 = 1 if presvote12==3
replace presvote2012 = 2 if presvote12==2
replace presvote2012 = 3 if presvote12==4

drop presvote12

recode ideology_post12 (1=.) (2=1) (3=2) (4=3) (5=4) (6=5) (7=6) (8=7), gen(ideopost12)

drop ideology_post12

merge 1:1 id using "data/iscap_wave8.dta"
gen nomidtermpre = _merge==1

drop _merge

gen pidsum14 = 10-pidpre14

merge 1:1 id using "data/iscap_wave9.dta"

gen nomidtermpost = _merge==1
drop _merge

recode vote14 (1=.) (2=1) (3=1) (4=1) (5=0), gen(novote14)
merge 1:1 id using "data/iscap_wave12.dta"

tab prespref16


recode prespref16 (1=.) (2=2) (3=1) (4=3) (5=3) (6=3) (7=.)
label values prespref16 .

label var prespref16 "1=Clinton, 2=Trump, 3=Other"


svyset [pweight=weight]


gen pid_12_3cat = .
replace pid_12_3cat = 1 if pid_base12==2
replace pid_12_3cat = -1 if pid_base12==3
replace pid_12_3cat = 1 if pid_lean12==2
replace pid_12_3cat = -1 if pid_lean12==3
replace pid_12_3cat = 0 if pid_base12==4 & pid_lean12!=2 & pid_lean12!=3
replace pid_12_3cat = 0 if pid_base12==5 & pid_lean12!=2 & pid_lean12!=3
replace pid_12_3cat = 0 if pid_base12==6 & pid_lean12!=2 & pid_lean12!=3

gen pid_16_3cat = .
replace pid_16_3cat = 1 if pid_base16==2
replace pid_16_3cat = -1 if pid_base16==3
replace pid_16_3cat = 1 if pid_lean16==2
replace pid_16_3cat = -1 if pid_lean16==3
replace pid_16_3cat = 0 if pid_base16==4 & pid_lean16!=2 & pid_lean16!=3
replace pid_16_3cat = 0 if pid_base16==5 & pid_lean16!=2 & pid_lean16!=3
replace pid_16_3cat = 0 if pid_base16==6 & pid_lean16!=2 & pid_lean16!=3

tab pid_12_3cat pid_16_3cat, cell
gen switch = 0 if pid_12_3cat!=. & pid_16_3cat!=.
replace switch = 1 if pid_12_3cat==-1 & pid_16_3cat==1
replace switch = 1 if pid_12_3cat==1 & pid_16_3cat==-1

tab switch [iw=weight]

gen pid_12_4cat = .
replace pid_12_4cat = 3 if pid_base12==4
replace pid_12_4cat = 3 if pid_base12==6
replace pid_12_4cat = 4 if pid_base12==5
replace pid_12_4cat = 1 if pid_12_3cat==-1
replace pid_12_4cat = 2 if pid_12_3cat==1

gen pid_16_4cat = .
replace pid_16_4cat = 3 if pid_base16==4
replace pid_16_4cat = 3 if pid_base16==6
replace pid_16_4cat = 4 if pid_base16==5
replace pid_16_4cat = 1 if pid_16_3cat==-1
replace pid_16_4cat = 2 if pid_16_3cat==1

gen repstrength12 = .
replace repstrength12 = 1 if pid_12_4cat==2
replace repstrength12 = 2 if rep_strength12==2
replace repstrength12 = 3 if rep_strength12==3

gen demstrength12 = .
replace demstrength12 = 1 if pid_12_4cat==1
replace demstrength12 = 2 if dem_strength12==2
replace demstrength12 = 3 if dem_strength12==3

gen repstrength16 = .
replace repstrength16 = 1 if pid_16_4cat==2
replace repstrength16 = 2 if rep_strength16==2
replace repstrength16 = 3 if rep_strength16==3

gen demstrength16 = .
replace demstrength16 = 1 if pid_16_4cat==1
replace demstrength16 = 2 if dem_strength16==2
replace demstrength16 = 3 if dem_strength16==3

gen pidsum2012 = 1 if demstrength12==3
replace pidsum2012 = 2 if demstrength12==2
replace pidsum2012 = 3 if demstrength12==1
replace pidsum2012 = 4 if pid_12_3cat==0
replace pidsum2012 = 5 if repstrength12==1
replace pidsum2012 = 6 if repstrength12==2
replace pidsum2012 = 7 if repstrength12==3

gen pidsumpost2012 = 1 if pid_post12_demstrength==2
replace pidsumpost2012 = 2 if pid_post12_base==3 & pidsumpost2012==.
replace pidsumpost2012 = 3 if pid_post12_indlean==3
replace pidsumpost2012 = 5 if pid_post12_indlean==2

replace pidsumpost2012 = 4 if pid_post12_base==4 & pidsumpost2012==.
replace pidsumpost2012 = 4 if pid_post12_base==5 & pidsumpost2012==.
replace pidsumpost2012 = 4 if pid_post12_base==6 & pidsumpost2012==.

replace pidsumpost2012 = 7 if pid_post12_repstrength==2
replace pidsumpost2012 = 6 if pid_post12_base==2 & pidsumpost2012==.

tab pidsumpost2012

gen pidsum2016 = 1 if demstrength16==3
replace pidsum2016 = 2 if demstrength16==2
replace pidsum2016 = 3 if demstrength16==1
replace pidsum2016 = 4 if pid_16_3cat==0
replace pidsum2016 = 5 if repstrength16==1
replace pidsum2016 = 6 if repstrength16==2
replace pidsum2016 = 7 if repstrength16==3

gen thirdparty2012 = pid_12_4cat==4
replace thirdparty2012 = . if pid_12_4cat==.

gen thirdparty2016 = pid_16_4cat==4
replace thirdparty2016 = . if pid_16_4cat==.


**** OBSERVED ESTIMATES/COUNTS

gen partyidswitch = 0 if pidsum2012>=1 & pidsum2012<=7 
replace partyidswitch = 1 if pidsum2012<=3 & pidsum2016>=5 & pidsum2016<=7
replace partyidswitch = 1 if  pidsum2016<=3 & pidsum2012>=5 & pidsum2012<=7
replace partyidswitch = . if pidsum2016<1
replace partyidswitch = . if pidsum2016>7 

svy: proportion partyidswitch

gen presprefswitch = 0 if presvote2012>0 & presvote2012<10
replace presprefswitch = 1 if presvote2012==1 & prespref16==2
replace presprefswitch = 1 if presvote2012==2 & prespref16==1
replace presprefswitch = . if prespref16<1
replace presprefswitch = . if prespref16>3
svy: proportion presprefswitch

drop partyidswitch presprefswitch


*** MI Estimates

tab age, gen(agedum)

tab region
recode region 2=1 4=3 7=6 8=9
tab region, gen(regiondum)
gen black = ethnicity==2
gen hispanic = ethnicity==4
gen othereth = ethnicity==5

gen highschool = education>=2
gen collegegrad = education==4

recode demstrength12 .=0
recode repstrength12 .=0
recode demstrength16 .=0
recode repstrength16 .=0
tab pid_12_4cat region

mi set mlong

mi register imputed pidsum2012 pidsumpost2012 pidsum14 pidsum2016 thirdparty2012 demstrength12 repstrength12 demstrength16 repstrength16 prespref12 newromney newobama obamatherm ideo12 ideopost12 novote2012 presvote2012 prespref16 

mi misstable nested

mi misstable summarize

mi impute chained (ologit, ascontinuous omit(novote2012 i.presvote2012 i.pidsum2016 i.prespref16 i.pidsum14 obamatherm ideopost12 i.pidsumpost2012)) ideo12 (ologit, omit (novote2012  i.pidsum2016 i.presvote2012 i.pidsum14 i.prespref16 obamatherm ideopost12 i.pidsumpost2012)) pidsum2012  (truncreg, ll(0) ul(100) omit(i.pidsum2016 i.prespref16 i.pidsum14 obamatherm ideopost12 i.pidsumpost2012)) newobama newromney  (logit, ascontinuous omit(i.pidsum2016 i.prespref16 i.pidsum14 i.presvote2012 obamatherm ideopost12 i.pidsumpost2012)) novote2012  (ologit, ascontinuous omit(i.pidsum2016 i.prespref16 i.prespref12 i.pidsum14 obamatherm i.pidsumpost2012)) ideopost12 (ologit, omit( ideo12  i.pidsum2016 i.prespref12 i.pidsum14 i.prespref16 obamatherm)) pidsumpost2012  (mlogit, augment omit(i.pidsum2016 i.prespref16 i.pidsum14 i.pidsumpost2012 novote2012 i.presvote2012 obamatherm ideopost12 i.pidsumpost2012)) prespref12 (mlogit, augment omit(i.pidsum2016 i.prespref16 i.pidsum14 obamatherm novote2012 ideo12 i.pidsum2012)) presvote2012 (ologit,  omit(i.prespref12  i.prespref16 i.pidsum2016 i.pidsum2012 ideo12 obamatherm14)) pidsum14 (truncreg, ll(0) ul(100) omit(i.pidsum2016 i.prespref16  i.presvote2012 ideo12 i.pidsum2012)) obamatherm14 (ologit, omit(i.pidsum2012 i.pidsumpost2012 ideo12 i.presvote2012 i.prespref12)) pidsum2016 (mlogit, augment omit(i.pidsum2012 i.prespref12 newobama i.pidsumpost2012 i.pidsum14 ideo12)) prespref16 = agedum1-agedum3 black hispanic othereth highschool collegegrad regiondum2-regiondum5, add(40) burnin(20) rseed(1234)  chaindots

mi estimate: svy: proportion pidsum2012
mi estimate: svy: proportion pidsum2016


mi passive: gen partyidswitch = 0
mi passive: replace partyidswitch = 1 if pidsum2012<=3 & pidsum2016>=5 & pidsum2016<=7
mi passive: replace partyidswitch = 1 if  pidsum2016<=3 & pidsum2012>=5 & pidsum2012<=7

mi estimate: svy: proportion partyidswitch

mi estimate: svy: proportion partyidswitch, over(demstrength12)
mi estimate: svy: proportion partyidswitch, over(repstrength12)
mi estimate: svy: proportion prespref16, over(partyidswitch)
mi estimate: svy: proportion partyidswitch, over(prespref16)


mi estimate: svy: proportion presvote2012
mi estimate: svy: proportion prespref16


mi passive: gen presprefswitch = 0 & presvote2012>=1 & presvote2012<=3
mi passive: replace presprefswitch = 1 if presvote2012==1 & prespref16==2
mi passive: replace presprefswitch = 1 if presvote2012==2 & prespref16==1

mi estimate: svy: proportion presprefswitch

